import { Config, get } from '../components/index.js'
import plugin from '../../../lib/plugins/plugin.js'
import cfg from '../../../lib/config/config.js'
import common from '../../../lib/common/common.js'
const path = process.cwd()
import {createRequire} from "module";
const require = createRequire(import.meta.url);
const fs =require('fs')
var Template = { "shield": [] };
var json = {}
var user = {}

let chuo_set = Config.chuo
let reply_text = chuo_set.text
let reply_img = chuo_set.img
let reply_voice = chuo_set.voice
let mutepick = chuo_set.mute
let example = chuo_set.example
const chuo_path = chuo_set.path
let jpg_number = chuo_set.jpg
let gif_number = chuo_set.gif
let voice_list = chuo_set.voiceURL
let word_list= JSON.parse(fs.readFileSync(`./plugins/js-box-plugin/config/data/system/chuo.json`, "utf8"));

await botfrom()
export class chuo extends plugin{
    constructor(){
    super({
        name: '戳一戳',
        dsc: '戳一戳机器人触发效果',
        event: 'notice.group.poke',
        priority: 50003,
        rule: [{ fnc: 'box_chuo' }]
    })
}

async box_chuo(e) {
  /*条件响应*/
  let set = await Config.admin_set
  let isplugin = set.zhenxunchuo
  let issleep = set.sleep
  var id = e.group_id
  var uid =e.user_id
  json = await get.sleeping(id, json, false)
  user = await get.shield(id, user, 'shield', Template, false)
  if (isplugin) {
     if (json[id].listen && issleep || (user[e.group_id].shield.indexOf(uid) !== -1) && issleep) return logger.warn('[js-box-plugin][zhenxunchuo][戳一戳已拦截]')
  } else {
     return true
  }
  /*主体*/
  logger.info('[js-box-plugin][zhenxunchuo][戳一戳生效]')
  if(e.target_id == cfg.qq){
  //生成0-100的随机数
  let random_type = Math.random()        
  //回复随机文字
  if(random_type < reply_text){
    let text_number = Math.ceil(Math.random() * word_list['length'])
    await e.reply(word_list[text_number-1])
  } else if (random_type < (reply_text + reply_img)) { //随机图片
    let photo_number = Math.ceil(Math.random() * (jpg_number + gif_number))
    if (photo_number<=jpg_number) {
      e.reply(segment.image('file:///' + chuo_path + photo_number + '.jpg'))
    } else {
      photo_number = photo_number - jpg_number
      e.reply(segment.image('file:///' + chuo_path + photo_number + '.gif'))
    }
  } else if (random_type < (reply_text + reply_img + reply_voice)) {
  //随机语言
     let voice_number = Math.ceil(Math.random() * word_list['length'])
     let url = voice_list[voice_number-1]
     await e.reply(segment.record(url))
  } else if (random_type < (reply_text + reply_img + reply_voice + mutepick)) {
     //两种禁言方式，随机选一种
     let mutetype = Math.ceil(Math.random() * 2)
     if (mutetype == 1) {
       e.reply('说了不要戳了！')
       await common.sleep(1000)
       await e.group.muteMember(e.operator_id,60);
       await common.sleep(3000)
       e.reply('啧')
       //有这个路径的图话可以加上
       await e.reply(segment.image('file:///' + path + '/resources/chuochuo/'+'laugh.jpg'))
     } else if (mutetype == 2) {
       e.reply('不！！')
       await common.sleep(500);
       e.reply('准！！')
       await common.sleep(500);
       e.reply('戳！！')
       await common.sleep(1000);
       await e.group.muteMember(e.operator_id,60)
     }
   } else if(random_type < (reply_text + reply_img + reply_voice + mutepick + example)){
     //表情包
     await e.reply(await segment.image(`http://ovooa.com/API/face_pat/?QQ=${e.operator_id}`))
     } else {
     e.reply('反击！')
     await common.sleep(1000)
     await e.group.pokeMember(e.operator_id)
     }
   }
 }
}

async function botfrom() {
  if (Bot.config.platform == 3) {
    return logger.warn('[js-box-plugin][zhenxunchuo]手表协议暂不支持戳一戳，请更换协议后重试')
  } else   if (Bot.config.platform == 4) {
    return logger.warn('[js-box-plugin][zhenxunchuo]Mac OS协议暂不支持戳一戳，请更换协议后重试')
  }
}